NO_COLOR=\033[0m
SKIP_COLOR=\033[33;01m
OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m

CGNSDIR = ..
include $(CGNSDIR)/make.defs
include $(CGNSDIR)/cgnsBuild.defs

CGNSLIB = $(CGNSDIR)/$(LIBCGNS)

COPTS  = $(CFLAGS) -I$(CGNSDIR)
FOPTS  = $(FFLAGS) -I$(CGNSDIR)
LDLIBS = $(CGNSLIB) $(BUILDLIBS)

#----------

CALL = \
	cgioc$(EXE) \
	cgioc_hdf$(EXE) \
	elemtest$(EXE) \
	ser_benchmark_hdf5$(EXE) \
	test_core_vfd$(EXE) \
	test_exts$(EXE) \
	test_goto$(EXE) \
	test_family_tree$(EXE) \
	test_multifam$(EXE) \
	test_partial$(EXE) \
	test_general_reshape$(EXE) \
	test_general_rind$(EXE) \
	test_ver31$(EXE) \
	write_array$(EXE) \
	write_links$(EXE) \
	write_bcdata$(EXE) \
	write_test$(EXE) \
	write_zones$(EXE) \
	write_rind$(EXE) \
	test_bbox$(EXE) \
	test_convert_elem$(EXE) \
	test_back_comp$(EXE) \
	test_complex$(EXE)

F_TESTS = \
	cgiof_F03$(EXE) \
	cgwrite_F03$(EXE) \
	cgread_F03$(EXE) \
	cgzconn_F03$(EXE) \
	cgsubreg_F03$(EXE) \
	test_general_rindf$(EXE)\
	test_family_treef$(EXE) \
	test_bboxf$(EXE) \
	test_complexf$(EXE)

FALL = $(F_TESTS)

CALL64 = test64c$(EXE)
FALL64 = test64f$(EXE) test64f_F03$(EXE)

ALL = dbtest$(EXE) open_cgns$(EXE) $(CALL) @FALL@
ALL64 = $(CALL64) @FALL64@

TESTS = $(CALL) @FALL@

TIMING_AVAIL:=$(shell /usr/bin/time -a -o CGNS_timing.txt -f "%e" pwd > /dev/null; echo $$?)
ifeq ("$(TIMING_AVAIL)" , "0")
  TIMING = /usr/bin/time -a -o CGNS_timing.txt -f "$$x %e" ./$$x >/dev/null 2>&1
else
  TIMING = ./$$x >/dev/null 2>&1
endif

#----------

all : $(ALL)
fortran : $(FALL)
test64 : $(ALL64)

#----------

test : $(TESTS)
	@echo " "; \
	status=0; \
	echo "=== running tests ==="; \
	if test -f CGNS_timing.txt; then \
	  $(RM) CGNS_timing.txt; \
	fi; \
	padlimit=40; \
	pad=$$(printf '%*s' "$$padlimit"); \
	pad=$${pad// /.}; \
        padlength=32; \
	time=""; \
	for x in $(TESTS) ; do \
	   printf "$$x"; \
	   printf ' %*.*s' 0 $$((padlength - $${#x} )) "$$pad"; \
	   if `$(TIMING)` ; then \
	     printf " [$(OK_COLOR)PASSED$(NO_COLOR)]"; \
             if [ -f "CGNS_timing.txt" ]; then \
               itime=$$(tail -n1 CGNS_timing.txt |  awk  '{print $$2}' | sed -e 's/$$/ sec/'); \
	       printf " %+12s" "$$itime";\
             fi; \
             printf "\n";\
	   else \
	     err=$$?; \
	     if [[ $$err -eq 125 ]]; then \
		printf " [$(SKIP_COLOR) SKIP $(NO_COLOR)]\n"; \
	     else \
		printf " [$(ERROR_COLOR)FAILED$(NO_COLOR)]\n"; \
		status=`expr $$status + 1`; \
	     fi; \
	   fi; \
	 done; \
	 echo "=== finished ==="; \
         if [ $$status -ne 0 ]; then \
           printf "$(ERROR_COLOR)"; \
         else \
           printf "$(OK_COLOR)"; \
         fi; \
         printf "$$status tests failed $(NO_COLOR) \n"; \
	 exit $$status;

#----------

cgioc$(EXE) : cgioc.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ cgioc.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@
#----------

cgioc_hdf$(EXE) : cgioc_hdf.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ cgioc_hdf.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

dbtest$(EXE) : dbtest.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ dbtest.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
dbtest.$(O) : dbtest.c utils.h
	$(CC) $(COPTS) -c dbtest.c

#----------

elemtest$(EXE) : elemtest.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ elemtest.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

open_cgns$(EXE) : open_cgns.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ open_cgns.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
open_cgns.$(O) : open_cgns.c utils.h
	$(CC) $(COPTS) -c open_cgns.c

#----------

ser_benchmark_hdf5$(EXE) : ser_benchmark_hdf5.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ ser_benchmark_hdf5.c $(LDLIBS) $(CLIBS) -lm
	$(STRIP) $@
#----------

test_core_vfd$(EXE) : test_core_vfd.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_core_vfd.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_exts$(EXE) : test_exts.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_exts.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_partial$(EXE) : test_partial.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_partial.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_general_reshape$(EXE) : test_general_reshape.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_general_reshape.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_general_rind$(EXE) : test_general_rind.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_general_rind.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_goto$(EXE) : test_goto.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_goto.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_family_tree$(EXE) : test_family_tree.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_family_tree.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_multifam$(EXE) : test_multifam.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_multifam.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_ver31$(EXE) : test_ver31.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_ver31.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

write_array$(EXE) : write_array.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_array.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_array.$(O) : write_array.c utils.h
	$(CC) $(COPTS) -c write_array.c

#----------

write_links$(EXE) : write_links.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_links.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_links.$(O) : write_links.c utils.h
	$(CC) $(COPTS) -c write_links.c

#----------

write_bcdata$(EXE) : write_bcdata.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_bcdata.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_bcdata.$(O) : write_bcdata.c utils.h
	$(CC) $(COPTS) -c write_bcdata.c

#----------

write_test$(EXE) : write_test.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ write_test.c $(LDLIBS) $(CLIBS) -lm
	$(STRIP) $@

#----------

write_zones$(EXE) : write_zones.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_zones.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_zones.$(O) : write_zones.c utils.h
	$(CC) $(COPTS) -c write_zones.c

#----------

write_rind$(EXE) : write_rind.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ write_rind.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_bbox$(EXE) : test_bbox.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_bbox.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_complex$(EXE) : test_complex.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_complex.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_convert_elem$(EXE) : test_convert_elem.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_convert_elem.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_back_comp$(EXE) : test_back_comp.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_back_comp.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

cgiof$(EXE) : cgiof.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgiof.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgiof_F03$(EXE) : cgiof_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgiof_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgwrite$(EXE) : cgwrite.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgwrite.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgwrite_F03$(EXE) : cgwrite_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgwrite_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgread$(EXE) : cgread.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgread.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgread_F03$(EXE) : cgread_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgread_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgzconn$(EXE) : cgzconn.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgzconn.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgzconn_F03$(EXE) : cgzconn_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgzconn_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgsubreg$(EXE) : cgsubreg.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgsubreg.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgsubreg_F03$(EXE) : cgsubreg_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgsubreg_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@
#----------

test64c$(EXE) : test64c.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ test64c.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
test64c.$(O) : test64c.c utils.h
	$(CC) $(COPTS) -c test64c.c

#----------

test64f$(EXE) : test64f.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test64f.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

test64f_F03$(EXE) : test64f_f03.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test64f_f03.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_general_rindf$(EXE) : test_general_rindf.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_general_rindf.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_family_treef$(EXE) : test_family_treef.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_family_treef.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_bboxf$(EXE) : test_bboxf.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_bboxf.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_complexf$(EXE) : test_complexf.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_complexf.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

utils.$(O) : utils.c utils.h
	$(CC) $(COPTS) -c utils.c

clean :
	-$(RM) *.$(O)

allclean : clean
	-$(RM) $(ALL) $(FALL) $(ALL64)
	-$(RM) *.cgns *.cgio
	-$(RM) CGNS_timing.txt

